package com.exprecards.common.web;

import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import com.exprecards.common.persistence.DAOFactory;
import com.exprecards.common.persistence.dto.ExpTbUsuario;
import com.exprecards.common.persistence.dto.ExpTbUsuarioPk;
import com.exprecards.common.persistence.exceptions.ExpTbUsuarioDaoException;

@ManagedBean(name = "crudUsuarioBean")
@ViewScoped
public class CRUDUsuarioBean extends GenericCrudBean<ExpTbUsuario> {

	private static final long serialVersionUID = 1L;

	
	@ManagedProperty("#{userSessionBean}")
	private UserSessionBean userSessionBean;
	
	
	private int tipoUsuario;
	
	private boolean  showPopupPanelDuplicados = true;
	
	public boolean isShowPopupPanelDuplicados() {
		return showPopupPanelDuplicados;
	}

	public void setShowPopupPanelDuplicados(boolean showPopupPanelDuplicados) {
		this.showPopupPanelDuplicados = showPopupPanelDuplicados;
	}

	public CRUDUsuarioBean() throws ExpTbUsuarioDaoException{
		super();
		
		FacesContext facesContext = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
		
		userSessionBean = (UserSessionBean)session.getAttribute("userSessionBean");
		
		this.lista = consultar();
		this.listaSimple = consultarSimple();
	}
	
	@Override
	protected boolean create() throws Exception{
		
		boolean result = false;
		
		this.valorActual.setEstado("A");
		
		DAOFactory.getExpTbUsuarioDao().insert(this.valorActual);
			
		result=true;
		
		return result;
	}
	
	@Override
	protected boolean update() throws Exception{
		
		boolean result = false;
		
		DAOFactory.getExpTbUsuarioDao().update(new ExpTbUsuarioPk(this.valorActual.getId()),this.valorActual);
		return result;
	}

	@Override
	protected void editable(int id) throws Exception{
		valorActual = DAOFactory.getExpTbUsuarioDao().findByPrimaryKey(id);
	}

	@Override
	protected void delete(int id) throws Exception{
		DAOFactory.getExpTbUsuarioDao().delete(new ExpTbUsuarioPk(id));
	}

	@Override
	protected List<Map<String, String>> consultar() {
		
		return null;
	}

	
	@Override
	protected ExpTbUsuario[] consultarSimple() throws ExpTbUsuarioDaoException {
			return DAOFactory.getExpTbUsuarioDao().findAll();
	}
	
	@Override
	protected ExpTbUsuario createNew() {
		return new ExpTbUsuario();
	}

	@Override
	protected boolean isId() {
		boolean result = false;
		if(valorActual.getId()==0){
			result = false;  
		}
		else{
			result = true;
		}
		return result;
	}
	
	@Override
	protected void clean(){
		
	}
	

	/**
	 * @return the userSessionBean
	 */
	public UserSessionBean getUserSessionBean() {
		return userSessionBean;
	}

	/**
	 * @param userSessionBean the userSessionBean to set
	 */
	public void setUserSessionBean(UserSessionBean userSessionBean) {
		this.userSessionBean = userSessionBean;
	}
	

	public int getTipoUsuario() {
		return tipoUsuario;
	}

	public void setTipoUsuario(int tipoUsuario) {
		this.tipoUsuario = tipoUsuario;
	}

}
